/*
 * ProcurarFamilia.java
 *
 * Created on 19/06/2011, 17:53:23
 */

package pbf.dialog;

import java.util.ArrayList;
import javax.swing.ButtonGroup;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Familia;
import pbf.conf.hibernate.entidades.Localidade;
import pbf.util.Mensagem;

/**
 *
 * @author Ricardo
 */
public class ProcurarFamilia extends javax.swing.JDialog {

    private ButtonGroup grupoBotao = new ButtonGroup();
    private static Object[] objetos = new Object[2];
    private static Session sessao = null;
    private ArrayList<Familia> listaFamilia = null;
    public Familia familia = null;
    private DetalhesFamilia detalhesFamilia = new DetalhesFamilia(null, true);

    public ProcurarFamilia(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        grupoBotao.add(radioCpf);
        grupoBotao.add(radioNis);
        grupoBotao.add(radioNome);
        sessao = HibernateUtil.openSession();
    }

    private void selectRadio(){
        //Desabilita os campos
        familiaCpf.setEditable(false);
        familiaNis.setEditable(false);
        familiaNome.setEditable(false);

        //Habilita campo selecionado
        if(radioNome.isSelected())
            familiaNome.setEditable(true);
        if(radioCpf.isSelected())
            familiaCpf.setEditable(true);
        if(radioNis.isSelected())
            familiaNis.setEditable(true);
    }

    private String getQuery(){
        if (radioNome.isSelected()) {
            return ("From Familia WHERE FAMILIA_ATIVO='s' AND FAMILIA_NOME LIKE '"+familiaNome.getText()+"%'");
        }
        if (radioCpf.isSelected()) {
            return ("From Familia WHERE FAMILIA_ATIVO='s' AND FAMILIA_CPF='"+familiaCpf.getText()+"' ");
        }
        if (radioNis.isSelected()) {
            return ("From Familia WHERE FAMILIA_ATIVO='s' AND FAMILIA_NIS='"+familiaNis.getText()+"' ");
        }
        return "";
    }

    private void setTable()
    {
        DefaultTableModel model = (DefaultTableModel) tabela.getModel();
        model.setNumRows(0);

        sessao.beginTransaction();
        listaFamilia = (ArrayList<Familia>) sessao.createQuery(getQuery()).list();

        for(Familia f : listaFamilia)
        {
            objetos[0] = f.getFamiliaNome();
            objetos[1] = f.getFamiliaCpf();

            model.addRow(objetos);
        }
        sessao.getTransaction().commit();
    }

    public Familia getFamilia(){
        setVisible(true);
        return familia;
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel4 = new javax.swing.JLabel();
        jSeparator9 = new javax.swing.JSeparator();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        jLabel20 = new javax.swing.JLabel();
        familiaNome = new javax.swing.JTextField();
        familiaNis = new javax.swing.JTextField();
        radioNome = new javax.swing.JRadioButton();
        familiaCpf = new javax.swing.JTextField();
        radioNis = new javax.swing.JRadioButton();
        radioCpf = new javax.swing.JRadioButton();
        jSeparator1 = new javax.swing.JSeparator();
        jButton2 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });
        getContentPane().setLayout(null);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 16));
        jLabel4.setText("Informe os dados da Família Abaixo");
        getContentPane().add(jLabel4);
        jLabel4.setBounds(20, 20, 360, 20);
        getContentPane().add(jSeparator9);
        jSeparator9.setBounds(20, 40, 450, 10);

        jLabel18.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel18.setText("CPF:");
        getContentPane().add(jLabel18);
        jLabel18.setBounds(20, 110, 170, 20);

        jLabel19.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel19.setText("Nome do Resposável:");
        getContentPane().add(jLabel19);
        jLabel19.setBounds(20, 50, 170, 20);

        jLabel20.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel20.setText("NIS:");
        getContentPane().add(jLabel20);
        jLabel20.setBounds(20, 80, 170, 20);

        familiaNome.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(familiaNome);
        familiaNome.setBounds(190, 50, 240, 23);

        familiaNis.setEditable(false);
        familiaNis.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(familiaNis);
        familiaNis.setBounds(190, 80, 240, 23);

        radioNome.setSelected(true);
        radioNome.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioNomeActionPerformed(evt);
            }
        });
        getContentPane().add(radioNome);
        radioNome.setBounds(440, 50, 21, 21);

        familiaCpf.setEditable(false);
        familiaCpf.setFont(new java.awt.Font("Tahoma", 0, 14));
        getContentPane().add(familiaCpf);
        familiaCpf.setBounds(190, 110, 240, 23);

        radioNis.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioNisActionPerformed(evt);
            }
        });
        getContentPane().add(radioNis);
        radioNis.setBounds(440, 80, 21, 21);

        radioCpf.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                radioCpfActionPerformed(evt);
            }
        });
        getContentPane().add(radioCpf);
        radioCpf.setBounds(440, 110, 21, 21);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 260, 500, 10);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/search.png"))); // NOI18N
        jButton2.setText("Pesquisar");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton2);
        jButton2.setBounds(200, 270, 130, 25);

        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Nome do Responsável", "CPF"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tabela.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tabelaMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(300);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(80);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 140, 450, 90);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/accept.png"))); // NOI18N
        jButton1.setText("Continuar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(335, 270, 130, 25);

        jLabel1.setText("Clique duas vezes na linha para vê os detalhes da Família.");
        getContentPane().add(jLabel1);
        jLabel1.setBounds(20, 230, 450, 14);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-508)/2, (screenSize.height-346)/2, 508, 346);
    }// </editor-fold>//GEN-END:initComponents

    private void radioNomeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioNomeActionPerformed
    selectRadio();
    }//GEN-LAST:event_radioNomeActionPerformed

    private void radioNisActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioNisActionPerformed
    selectRadio();
    }//GEN-LAST:event_radioNisActionPerformed

    private void radioCpfActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_radioCpfActionPerformed
    selectRadio();
    }//GEN-LAST:event_radioCpfActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    setTable();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
    sessao.close();
    }//GEN-LAST:event_formWindowClosing

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    try{
        familia = listaFamilia.get(tabela.getSelectedRow());
        dispose();
    }catch(Exception e){
        Mensagem.mensagerio("Selecione um registro da Tabela para Continua: ", "Mensagem", Mensagem.ATENCAO_MENSAGEM);
    }
    }//GEN-LAST:event_jButton1ActionPerformed

    private void tabelaMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tabelaMouseClicked
        if (evt.getClickCount() == 2) {
            detalhesFamilia.setFormulario(listaFamilia.get(tabela.getSelectedRow()), listaFamilia.get(tabela.getSelectedRow()).getLocalidade());
            detalhesFamilia.setVisible(true);
        }
    }//GEN-LAST:event_tabelaMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField familiaCpf;
    private javax.swing.JTextField familiaNis;
    private javax.swing.JTextField familiaNome;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator9;
    private javax.swing.JRadioButton radioCpf;
    private javax.swing.JRadioButton radioNis;
    private javax.swing.JRadioButton radioNome;
    private javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables

}
